package bitmap;

import global.*;
import heap.InvalidSlotNumberException;
import heap.Tuple;

import java.io.IOException;
import java.lang.*;

import btree.*;
import bufmgr.*;
import diskmgr.*;
import iterator.*;

public class BM implements GlobalConst{
	
  /** For debug. Print the BitMap out
   *@param header  the head page of the BitMap file
   *@exception IOException error from the lower layer
   *@exception ConstructPageException  error from BM page constructor
   *@exception IteratorException  error from iterator
   *@exception HashEntryNotFoundException  error from lower layer
   *@exception InvalidFrameNumberException  error from lower layer
   *@exception PageUnpinnedException  error from lower layer
   *@exception ReplacerException  error from lower layer
 * @throws InvalidSlotNumberException 
 * @throws BufMgrException 
 * @throws PagePinnedException 
 * @throws BufferPoolExceededException 
 * @throws PageNotReadException 
 * @throws HashOperationException 
   */
  public static void printBitMap(BitMapHeaderPage header) //for debug
    throws IOException, 
	   ConstructPageException, 
	   IteratorException,
	   HashEntryNotFoundException,
	   InvalidFrameNumberException,
	   PageUnpinnedException,
	   ReplacerException, 
	   HashOperationException, 
	   PageNotReadException, 
	   BufferPoolExceededException, 
	   PagePinnedException, 
	   BufMgrException, 
	   InvalidSlotNumberException 
    {
      if(header.get_rootId().pid == INVALID_PAGE) 
      {
    	  System.out.println("The BitMap is Empty!!!");
    	  return;
	  }
      
    
      System.out.println("");
	      System.out.println("");
	      System.out.println("");
	      System.out.println("---------------The Bit Map Content---------------");
	      
	      _printBitMap(header.get_rootId());
	      
	      System.out.println("--------------- End ---------------");
	      System.out.println("");
	      System.out.println("");
	}
  
  
	  
	  private static void _printBitMap(PageId currentPageId) 
	    throws IOException, 
		   ConstructPageException, 
		   IteratorException,
		   HashEntryNotFoundException,
		   InvalidFrameNumberException,
		   PageUnpinnedException,
		   ReplacerException, 
		   HashOperationException, 
		   PageNotReadException, 
		   BufferPoolExceededException, 
		   PagePinnedException, 
		   BufMgrException, 
		   InvalidSlotNumberException 
	    {
		  
		  BMPage tempPage = new BMPage();

		  
		  SystemDefs.JavabaseBM.pinPage(currentPageId,tempPage,false/*Rdisk*/); //get the page from DB
		  
		  byte[] tempArray = null;
		  tempArray = tempPage.getBMpageArray(); //get the data array
		  
		  if(tempPage.getpage() != null)
		  {	
			  for(int i=0;i<tempArray.length;i++)
			  {
				  for(int j=0;j<8;j++)
				  {
					  System.out.print((tempArray[i]>>>j)&1);
				  }
				  System.out.print(" ");	//print the dataarray
			  }
			  System.out.print("\n");
			  SystemDefs.JavabaseBM.unpinPage(currentPageId , false/* not dirty*/);
			  _printBitMap(tempPage.getNextPage()); //continue to print the next page
		  }
		  
		else
			SystemDefs.JavabaseBM.unpinPage(currentPageId , false/*not dirty*/);  		  
	    }
}


